home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / KOOB / common / server / clientConnection.cs < prev    next >
Text File  |  2005-11-23  |  6KB  |  206 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine 
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5.  
  6.  
  7. //-----------------------------------------------------------------------------
  8. // This script function is called before a client connection
  9. // is accepted.  Returning "" will accept the connection,
  10. // anything else will be sent back as an error to the client.
  11. // All the connect args are passed also to onConnectRequest
  12. //
  13. function GameConnection::onConnectRequest( %client, %netAddress, %name )
  14. {
  15.    echo("Connect request from: " @ %netAddress);
  16.    if($Server::PlayerCount >= $pref::Server::MaxPlayers)
  17.       return "CR_SERVERFULL";
  18.    return "";
  19. }
  20.  
  21. //-----------------------------------------------------------------------------
  22. // This script function is the first called on a client accept
  23. //
  24. function GameConnection::onConnect( %client, %name )
  25. {
  26.    // Send down the connection error info, the client is
  27.    // responsible for displaying this message if a connection
  28.    // error occures.
  29.    messageClient(%client,'MsgConnectionError',"",$Pref::Server::ConnectionError);
  30.  
  31.    // Send mission information to the client
  32.    sendLoadInfoToClient( %client );
  33.  
  34.    // Simulated client lag for testing...
  35.    // %client.setSimulatedNetParams(0.1, 30);
  36.  
  37.    // Get the client's unique id:
  38.    // %authInfo = %client.getAuthInfo();
  39.    // %client.guid = getField( %authInfo, 3 );
  40.    %client.guid = 0;
  41.    addToServerGuidList( %client.guid );
  42.    
  43.    // Set admin status
  44.    if (%client.getAddress() $= "local") {
  45.       %client.isAdmin = true;
  46.       %client.isSuperAdmin = true;
  47.    }
  48.    else {
  49.       %client.isAdmin = false;
  50.       %client.isSuperAdmin = false;
  51.    }
  52.  
  53.    // Save client preferences on the connection object for later use.
  54.    %client.gender = "Male";
  55.    %client.armor = "Light";
  56.    %client.race = "Human";
  57.    %client.skin = addTaggedString( "base" );
  58.    %client.setPlayerName(%name);
  59.    %client.score = 0;
  60.  
  61.    // 
  62.    $instantGroup = ServerGroup;
  63.    $instantGroup = MissionCleanup;
  64.    echo("CADD: " @ %client @ " " @ %client.getAddress());
  65.  
  66.    // Inform the client of all the other clients
  67.    %count = ClientGroup.getCount();
  68.    for (%cl = 0; %cl < %count; %cl++) {
  69.       %other = ClientGroup.getObject(%cl);
  70.       if ((%other != %client)) {
  71.          // These should be "silent" versions of these messages...
  72.          messageClient(%client, 'MsgClientJoin', "", 
  73.                %other.name,
  74.                %other,
  75.                %other.sendGuid,
  76.                %other.score, 
  77.                %other.isAIControlled(),
  78.                %other.isAdmin, 
  79.                %other.isSuperAdmin);
  80.       }
  81.    }
  82.  
  83.    // Inform the client we've joined up
  84.    messageClient(%client,
  85.       'MsgClientJoin', '\c2Welcome to the Torque demo app %1.', 
  86.       %client.name, 
  87.       %client,
  88.       %client.sendGuid,
  89.       %client.score,
  90.       %client.isAiControlled(), 
  91.       %client.isAdmin, 
  92.       %client.isSuperAdmin);
  93.  
  94.    // Inform all the other clients of the new guy
  95.    messageAllExcept(%client, -1, 'MsgClientJoin', '\c1%1 joined the game.', 
  96.       %client.name, 
  97.       %client,
  98.       %client.sendGuid,
  99.       %client.score,
  100.       %client.isAiControlled(), 
  101.       %client.isAdmin, 
  102.       %client.isSuperAdmin);
  103.  
  104.    // If the mission is running, go ahead download it to the client
  105.    if ($missionRunning)
  106.       %client.loadMission();
  107.    $Server::PlayerCount++;
  108. }
  109.  
  110. //-----------------------------------------------------------------------------
  111. // A player's name could be obtained from the auth server, but for
  112. // now we use the one passed from the client.
  113. // %realName = getField( %authInfo, 0 );
  114. //
  115. function GameConnection::setPlayerName(%client,%name)
  116. {
  117.    %client.sendGuid = 0;
  118.  
  119.    // Minimum length requirements
  120.    %name = stripTrailingSpaces( strToPlayerName( %name ) );
  121.    if ( strlen( %name ) < 3 )
  122.       %name = "Poser";
  123.  
  124.    // Make sure the alias is unique, we'll hit something eventually
  125.    if (!isNameUnique(%name))
  126.    {
  127.       %isUnique = false;
  128.       for (%suffix = 1; !%isUnique; %suffix++)  {
  129.          %nameTry = %name @ "." @ %suffix;
  130.          %isUnique = isNameUnique(%nameTry);
  131.       }
  132.       %name = %nameTry;
  133.    }
  134.  
  135.    // Tag the name with the "smurf" color:
  136.    %client.nameBase = %name;
  137.    %client.name = addTaggedString("\cp\c8" @ %name @ "\co");
  138. }
  139.  
  140. function isNameUnique(%name)
  141. {
  142.    %count = ClientGroup.getCount();
  143.    for ( %i = 0; %i < %count; %i++ )
  144.    {
  145.       %test = ClientGroup.getObject( %i );
  146.       %rawName = stripChars( detag( getTaggedString( %test.name ) ), "\cp\co\c6\c7\c8\c9" );
  147.       if ( strcmp( %name, %rawName ) == 0 )
  148.          return false;
  149.    }
  150.    return true;
  151. }
  152.  
  153. //-----------------------------------------------------------------------------
  154. // This function is called when a client drops for any reason
  155. //
  156. function GameConnection::onDrop(%client, %reason)
  157. {
  158.    %client.onClientLeaveGame();
  159.    
  160.    removeFromServerGuidList( %client.guid );
  161.    messageAllExcept(%client, -1, 'MsgClientDrop', '\c1%1 has left the game.', %client.name, %client);
  162.  
  163.    removeTaggedString(%client.name);
  164.    echo("CDROP: " @ %client @ " " @ %client.getAddress());
  165.    $Server::PlayerCount--;
  166.    
  167.    // Reset the server if everyone has left the game
  168.    if( $Server::PlayerCount == 0 && $Server::Dedicated)
  169.       schedule(0, 0, "resetServerDefaults");
  170. }
  171.  
  172.  
  173. //-----------------------------------------------------------------------------
  174.  
  175. function GameConnection::startMission(%this)
  176. {
  177.    // Inform the client the mission starting
  178.    commandToClient(%this, 'MissionStart', $missionSequence);
  179. }
  180.  
  181.  
  182. function GameConnection::endMission(%this)
  183. {
  184.    // Inform the client the mission is done
  185.    commandToClient(%this, 'MissionEnd', $missionSequence);
  186. }
  187.  
  188.  
  189. //--------------------------------------------------------------------------
  190. // Sync the clock on the client.
  191.  
  192. function GameConnection::syncClock(%client, %time)
  193. {
  194.    commandToClient(%client, 'syncClock', %time);
  195. }
  196.  
  197.  
  198. //--------------------------------------------------------------------------
  199. // Update all the clients with the new score
  200.  
  201. function GameConnection::incScore(%this,%delta)
  202. {
  203.    %this.score += %delta;
  204.    messageAll('MsgClientScoreChanged', "", %this.score, %this);
  205. }
  206.